package com.tnxst.phexamination.adapter.follow;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;

import com.tnxst.phexamination.R;
import com.tnxst.phexamination.dto.SignDTO;
import com.tnxst.phexamination.dto.TbPatientRecordDTO;
import com.tnxst.phexamination.fragment.SignFragment;
import com.tnxst.phexamination.fragment.follow.FilingPhthisisFragment;
import com.tnxst.phexamination.utils.StringUtils;

import java.util.List;

public class TbPatientRecordAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;

    private FragmentManager fragmentManager;

    private List<TbPatientRecordDTO> tbPatientRecordDTOS;

    private FilingPhthisisFragment fragment;



    public TbPatientRecordAdapter(List<TbPatientRecordDTO> tbPatientRecordDTOS, FragmentManager fragmentManager) {
        this.fragmentManager = fragmentManager;
        this.tbPatientRecordDTOS = tbPatientRecordDTOS;
    }


    public void setTbPatientRecordDTOS(List<TbPatientRecordDTO> tbPatientRecordDTOS) {
        this.tbPatientRecordDTOS = tbPatientRecordDTOS;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view =  LayoutInflater.from(context).inflate(R.layout.tb_patient_record_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        ViewHolder holders = (ViewHolder) holder;
        TbPatientRecordDTO tbPatientRecordDTO = tbPatientRecordDTOS.get(position);

        holders.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showFragment(tbPatientRecordDTO);
            }
        });

        if (StringUtils.isNotNull(tbPatientRecordDTO.getPersonName()))
            holders.patientName.setText(tbPatientRecordDTO.getPersonName());

        if (StringUtils.isNotNull(tbPatientRecordDTO.getDoctorName()))
            holders.doctorName.setText(tbPatientRecordDTO.getDoctorName());

        if (StringUtils.isNotNull(tbPatientRecordDTO.getDiagnosisDate()))
            holders.diagnosisDate.setText(tbPatientRecordDTO.getDiagnosisDate());

        if (StringUtils.isNotNull(tbPatientRecordDTO.getTreatmentDate()))
            holders.treatmentDate.setText(tbPatientRecordDTO.getTreatmentDate());

        if (StringUtils.isNotNull(tbPatientRecordDTO.getInitialTreatment())){
            String initialTreatment = tbPatientRecordDTO.getInitialTreatment();
            switch (initialTreatment){
                case "1":
                    holders.initialTreatment.setText("新病人");
                    break;
                case "2":
                    holders.initialTreatment.setText("其他");
                    break;
            }
        }


        if (StringUtils.isNotNull(tbPatientRecordDTO.getPaymentMethod())){
            String paymentMethod = tbPatientRecordDTO.getPaymentMethod();
            switch (paymentMethod){
                case "1":
                    holders.paymentMethod.setText("社保");
                    break;
                case "2":
                    holders.paymentMethod.setText("自费");
                    break;
                case "3":
                    holders.paymentMethod.setText("免费");
                    break;
            }

        }


    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        View view;

        TextView patientName;

        TextView doctorName;

        TextView diagnosisDate;

        TextView treatmentDate;

        TextView initialTreatment;

        TextView paymentMethod;


        public ViewHolder(View view) {
            super(view);
            this.view = view;
            this.patientName = view.findViewById(R.id.textView166);
            this.doctorName = view.findViewById(R.id.textView168);
            this.diagnosisDate = view.findViewById(R.id.textView276);
            this.treatmentDate = view.findViewById(R.id.textView305);
            this.initialTreatment = view.findViewById(R.id.textView171);
            this.paymentMethod = view.findViewById(R.id.textView173);
        }
    }

    @Override
    public int getItemCount() {
        return tbPatientRecordDTOS.size();
    }


    private void showFragment(TbPatientRecordDTO tbPatientRecordDTO) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.setCustomAnimations(R.anim.fragment_slide_up, 0);
        fragment = new FilingPhthisisFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("tbPatientRecordDTO", tbPatientRecordDTO);
        fragment.setArguments(bundle);
        ft.replace(android.R.id.content, fragment);
        ft.show(fragment);
        ft.commitAllowingStateLoss();
    }

}
